package com.shrikanthravi.customnavigationdrawer.slice;

import com.shrikanthravi.customnavigationdrawer.ResourceTable;
import com.shrikanthravi.customnavigationdrawer.fraction.NewsFraction;
import com.shrikanthravi.customnavigationdrawer2.data.MenuItem;
import com.shrikanthravi.customnavigationdrawer2.utils.FractionChangeManager;
import com.shrikanthravi.customnavigationdrawer2.widget.SNavigationDrawer;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    SNavigationDrawer sNavigationDrawer;
    private final ArrayList<Fraction> mFragments = new ArrayList<>();
    private FractionChangeManager manager;
    private final String[] content={"News Fragment","Feed Fragment","Messages Fragment","Music Fragment"};
    private final int[] colors = {ResourceTable.Color_red,ResourceTable.Color_orange, ResourceTable.Color_green, ResourceTable.Color_colorBlue};

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        sNavigationDrawer = (SNavigationDrawer) findComponentById(ResourceTable.Id_navigationDrawer);
        List<MenuItem> menuItems = new ArrayList<>();
        menuItems.add(new MenuItem("News", ResourceTable.Media_news_bg));
        menuItems.add(new MenuItem("Feed", ResourceTable.Media_feed_bg));
        menuItems.add(new MenuItem("Messages", ResourceTable.Media_message_bg));
        menuItems.add(new MenuItem("Music", ResourceTable.Media_music_bg));
        sNavigationDrawer.setMenuItemList(menuItems);
        initFragment(menuItems);
        if (getAbility() instanceof FractionAbility) {
            FractionAbility ability = (FractionAbility) getAbility();
            manager = new FractionChangeManager(ability.getFractionManager(), ResourceTable.Id_frameLayout, mFragments);
        }
        sNavigationDrawer.setDrawerListener(new SNavigationDrawer.DrawerListener() {
            @Override
            public void onDrawerOpening() {
            }

            @Override
            public void onDrawerClosing() {

            }

            @Override
            public void onDrawerOpened() {

            }

            @Override
            public void onDrawerClosed() {

            }

            @Override
            public void onDrawerStateChanged(SNavigationDrawer.State newState) {
            }
        });
        sNavigationDrawer.setOnMenuItemClickListener(position -> manager.setFragments(position));
    }

    private void initFragment(List<MenuItem> menuItems) {
        for (int i = 0; i < menuItems.size(); i++) {
            mFragments.add(NewsFraction.getInstance( this,content[i], colors[i]));
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
